<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="mail" tabindex="-1"><a class="header-anchor" href="#mail"><span>Mail</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#driver-prerequisites">邮件驱动预备知识</a></li>
</ul>
</li>
<li><a href="#generating-mailables">生成可邮寄类</a></li>
<li><a href="#writing-mailables">编写可邮寄类</a>
<ul>
<li><a href="#configuring-the-sender">配置发件人</a></li>
<li><a href="#configuring-the-view">配置视图</a></li>
<li><a href="#view-data">视图数据</a></li>
<li><a href="#attachments">附件</a></li>
<li><a href="#inline-attachments">内联附件</a></li>
<li><a href="#customizing-the-swiftmailer-message">自定义 SwiftMailer 消息</a></li>
</ul>
</li>
<li><a href="#markdown-mailables">Markdown 邮件类</a>
<ul>
<li><a href="#generating-markdown-mailables">生成 Markdown 邮件类</a></li>
<li><a href="#writing-markdown-messages">编写 Markdown 消息</a></li>
<li><a href="#customizing-the-components">自定义组件</a></li>
</ul>
</li>
<li><a href="#sending-mail">发送邮件</a>
<ul>
<li><a href="#queueing-mail">邮件队列</a></li>
</ul>
</li>
<li><a href="#rendering-mailables">渲染可邮寄类</a>
<ul>
<li><a href="#previewing-mailables-in-the-browser">在浏览器中预览邮件</a></li>
</ul>
</li>
<li><a href="#localizing-mailables">本地化可邮寄类</a></li>
<li><a href="#mail-and-local-development">邮件 &amp; 本地开发</a></li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 基于 <a href="https://swiftmailer.symfony.com/" target="_blank" rel="noopener noreferrer">SwiftMailer</a> 库提供了一套干净、清爽的邮件 API。Laravel 为 SMTP、Mailgun、Postmark、Amazon SES，以及 <code v-pre>sendmail</code> 提供了驱动，从而允许你快速通过本地或云服务发送邮件。</p>
<h3 id="邮件驱动预备知识" tabindex="-1"><a class="header-anchor" href="#邮件驱动预备知识"><span>邮件驱动预备知识</span></a></h3>
<p>基于API的驱动程序（如 Mailgun 和 Postmark）通常比 SMTP 服务器更简单，更快捷。 如果可以的话，尽可能使用其中一个驱动程序。 所有API驱动程序都需要Guzzle HTTP库，可以通过Composer包管理器安装：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> guzzlehttp<span class="token operator">/</span>guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mailgun-驱动" tabindex="-1"><a class="header-anchor" href="#mailgun-驱动"><span>Mailgun 驱动</span></a></h4>
<p>要使用 Mailgun 驱动，首先安装 Guzzle，然后在配置文件 <code v-pre>config/mail.php</code> 中设置 <code v-pre>driver</code> 选项为 <code v-pre>mailgun</code> 。接下来，验证配置文件 <code v-pre>config/services.php</code> 包含如下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailgun'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'domain'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-mailgun-domain'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-mailgun-key'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您没有使用 “US” <a href="https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions" target="_blank" rel="noopener noreferrer">Mailgun地区</a> ，您可以在 <code v-pre>services</code> 配置文件中定义您所在地区的终端：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'mailgun'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'domain'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-mailgun-domain'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-mailgun-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'endpoint'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'api.eu.mailgun.net'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="postmark-驱动程序" tabindex="-1"><a class="header-anchor" href="#postmark-驱动程序"><span>Postmark 驱动程序</span></a></h4>
<p>要使用Postmark驱动程序，请通过 Composer 安装 Postmark 的 SwiftMailer：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> wildbit<span class="token operator">/</span>swiftmailer<span class="token operator">-</span>postmark</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，安装 Guzzle 并将 <code v-pre>config/mail.php</code> 配置文件中的 <code v-pre>driver</code> 选项设置为 <code v-pre>postmark</code>。 最后，验证您的 <code v-pre>config/services.php</code> 配置文件包含以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'postmark'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'token'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-postmark-token'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ses-驱动" tabindex="-1"><a class="header-anchor" href="#ses-驱动"><span>SES 驱动</span></a></h4>
<p>要使用 Amazon SES 驱动，先安装 Amazon AWS 的 PHP SDK，你可以通过添加如下行到 <code v-pre>composer.json</code> 文件的 <code v-pre>require</code> 部分然后运行 <code v-pre>composer update</code> 命令来安装该库：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"aws/aws-sdk-php"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"~3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，设置配置文件 <code v-pre>config/mail.php</code> 中的 <code v-pre>driver</code> 选项为 <code v-pre>ses</code> 。然后，验证配置文件 <code v-pre>config/services.php</code> 包含如下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ses'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-ses-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-ses-secret'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ses-region'</span><span class="token punctuation">,</span>  <span class="token comment">// e.g. us-east-1</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你在执行 SES 时需要包含 <a href="https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#sendrawemail" target="_blank" rel="noopener noreferrer">附加选项</a> <code v-pre>SendRawEmail</code>请求，可以在 <code v-pre>ses</code> 配置中定义 <code v-pre>options</code> 数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ses'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-ses-key'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'your-ses-secret'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ses-region'</span><span class="token punctuation">,</span>  <span class="token comment">// e.g. us-east-1</span></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'ConfigurationSetName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'MyConfigurationSet'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'Tags'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'Name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'Value'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="生成可邮寄类" tabindex="-1"><a class="header-anchor" href="#生成可邮寄类"><span>生成可邮寄类</span></a></h2>
<p>在 Laravel 中，应用发送的每一封邮件都可以表示为“可邮寄”类，这些类都存放在 <code v-pre>app/Mail</code> 目录。如果没看到这个目录，别担心，它将会在你使用 <code v-pre>make:mail</code> 命令创建第一个可邮寄类时生成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>mail OrderShipped</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="编写可邮寄类" tabindex="-1"><a class="header-anchor" href="#编写可邮寄类"><span>编写可邮寄类</span></a></h2>
<p>所有的可邮寄类配置都在 <code v-pre>build</code> 方法中完成，在这个方法中，你可以调用多个方法，例如 <code v-pre>from</code>，<code v-pre>subject</code>， <code v-pre>view</code>， 和 <code v-pre>attach</code> 来配置邮件的内容和发送。</p>
<h3 id="配置发件人" tabindex="-1"><a class="header-anchor" href="#配置发件人"><span>配置发件人</span></a></h3>
<h4 id="使用-from-方法" tabindex="-1"><a class="header-anchor" href="#使用-from-方法"><span>使用 <code v-pre>from</code> 方法</span></a></h4>
<p>我们来看一下邮件发件人的配置，或者，换句话说，邮件来自于谁。有两种方式来配置发送者，第一种方式是在可邮寄类的 <code v-pre>build</code> 方法方法中调用 <code v-pre>from</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用全局的-from-地址" tabindex="-1"><a class="header-anchor" href="#使用全局的-from-地址"><span>使用全局的 <code v-pre>from</code> 地址</span></a></h4>
<p>不过，如果你的应用在所有邮件中都使用相同的发送地址，在每个生成的可邮寄类中都调用 <code v-pre>from</code> 方法就显得很累赘。取而代之地，你可以在配置文件 <code v-pre>config/mail.php</code> 中指定一个全局的发送地址， 如果在 mailable 类中未指定其他 <code v-pre>from</code> 地址，则将使用此地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'from'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'App Name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此外，您可以在 <code v-pre>config/mail.php</code> 配置文件中定义全局 <code v-pre>reply_to</code> 地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'reply_to'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'App Name'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置视图" tabindex="-1"><a class="header-anchor" href="#配置视图"><span>配置视图</span></a></h3>
<p>你可以在可邮寄类的 <code v-pre>build</code> 方法中使用 <code v-pre>view</code> 方法来指定渲染邮件内容时使用哪个视图模板，由于每封邮件通常使用 <a href="https://learnku.com/docs/laravel/6.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板</a>来渲染内容，所以你可以在构建邮件 HTML 时使用 Blade 模板引擎提供的所有功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<blockquote>
<p>Tip：你可以创建一个 <code v-pre>resources/views/emails</code> 目录来存放所有邮件模板，当然，你也可以将邮件模板放到 <code v-pre>resources/views</code> 目录下任意其它位置。</p>
</blockquote>
</blockquote>
<h4 id="纯文本邮件" tabindex="-1"><a class="header-anchor" href="#纯文本邮件"><span>纯文本邮件</span></a></h4>
<p>如果你想要定义一个纯文本格式的邮件，可以使用 <code v-pre>text</code> 方法。和 <code v-pre>view</code> 方法一样， <code v-pre>text</code> 方法接收一个用于渲染邮件内容的模板名，你既可以定义纯文本消息也可以定义 HTML 消息</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped_plain'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="视图数据" tabindex="-1"><a class="header-anchor" href="#视图数据"><span>视图数据</span></a></h3>
<h4 id="通过公共属性" tabindex="-1"><a class="header-anchor" href="#通过公共属性"><span>通过公共属性</span></a></h4>
<p>通常，我们需要传递一些数据到渲染邮件的 HTML 视图以供使用。有两种方式将数据传递到视图，首先，您的 mailable 类中定义的任何公共属性将自动传递给视图。 因此，您可以将数据传递到 mailable 类的构造函数，并将该数据设置为类的公共属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 订单实例.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name">Order</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的消息实例.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span> <span class="token operator">=</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 构建消息.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>数据被设置给公共属性后，将会在视图中自动生效，所以你可以像在 Blade 模板中访问其它数据一样访问它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">price</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过-with-方法" tabindex="-1"><a class="header-anchor" href="#通过-with-方法"><span>通过 <code v-pre>with</code> 方法</span></a></h4>
<p>如果你想要在数据发送到模板之前自定义邮件数据的格式，可以通过 <code v-pre>with</code> 方法手动传递数据到视图。一般情况下，你还是需要通过可邮寄类的构造器传递数据，不过，这次你需要设置数据为 <code v-pre>protected</code> 或 <code v-pre>private</code> 属性，这样，这些数据就不会在视图中自动生效。然后，当调用 <code v-pre>with</code> 方法时，传递数组数据到该方法以便数据在视图模板中生效：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 订单实例.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name">Order</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的消息实例.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Order</span> <span class="token variable">$order</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span> <span class="token operator">=</span> <span class="token variable">$order</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 构建消息.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> $this</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'orderName'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token string single-quoted-string">'orderPrice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">price</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>数据通过 <code v-pre>with</code> 方法传递到视图后，将会在视图中自动生效，因此你也可以像在 Blade 模板访问其它数据一样访问传递过来的数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Price<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$orderPrice</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="附件" tabindex="-1"><a class="header-anchor" href="#附件"><span>附件</span></a></h3>
<p>要在邮件中加入附件，在 <code v-pre>build</code> 方法中使用 <code v-pre>attach</code> 方法。<code v-pre>attach</code> 方法接受文件的绝对路径作为它的第一个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>附加文件到消息时，你也可以传递 <code v-pre>数组</code> 给 <code v-pre>attach</code> 方法作为第二个参数，以指定显示名称和 / 或是 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="从磁盘中添加附件" tabindex="-1"><a class="header-anchor" href="#从磁盘中添加附件"><span>从磁盘中添加附件</span></a></h4>
<p>如果您已在<a href="https://learnku.com/docs/laravel/6.x/filesystem" target="_blank" rel="noopener noreferrer">文件存储</a>上存储了一个文件，则可以使用 <code v-pre>attachFromStorage</code> 方法将其附加到电子邮件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">               <span class="token operator">-></span><span class="token function">attachFromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如有必要，您可以使用 <code v-pre>attachFromStorage</code> 方法的第二个和第三个参数指定文件的附件名称和其他选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">               <span class="token operator">-></span><span class="token function">attachFromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                   <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span></span>
<span class="line">               <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要指定默认磁盘以外的存储磁盘，可以使用 <code v-pre>attachFromStorageDisk</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">               <span class="token operator">-></span><span class="token function">attachFromStorageDisk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="原始数据附件" tabindex="-1"><a class="header-anchor" href="#原始数据附件"><span>原始数据附件</span></a></h4>
<p><code v-pre>attachData</code> 可以使用字节数据作为附件。例如，你可以使用这个方法将内存中生成而没有保存到磁盘中的 PDF 附加到邮件中。<code v-pre>attachData</code> 方法第一个参数接收原始字节数据，第二个参数为文件名，第三个参数接受一个数组以指定其他参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachData</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">pdf</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="内联附件" tabindex="-1"><a class="header-anchor" href="#内联附件"><span>内联附件</span></a></h3>
<p>在邮件中嵌入内联图片通常都很麻烦；不过，Laravel 提供了向邮件中附加图片并获取适当的 CID 的简便方法。可以使用邮件模板中 <code v-pre>$message</code> 变量的 <code v-pre>embed</code> 方法来嵌入内联图片。Laravel 自动使 <code v-pre>$message</code> 变量在全部邮件模板中可用，不需要担心如何手动传递它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>body<span class="token operator">></span></span>
<span class="line">    Here is an image<span class="token punctuation">:</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string double-quoted-string">"{{ <span class="token interpolation"><span class="token variable">$message</span><span class="token operator">-></span><span class="token property">embed</span></span>(<span class="token interpolation"><span class="token variable">$pathToImage</span></span>) }}"</span><span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：<code v-pre>$message</code> 在文本消息中不可用，因为文本消息不能使用内联附件。</p>
</blockquote>
<h4 id="嵌入原始数据附件" tabindex="-1"><a class="header-anchor" href="#嵌入原始数据附件"><span>嵌入原始数据附件</span></a></h4>
<p>如果已经有了希望嵌入邮件模板的原始数据串，可以使用 <code v-pre>$message</code> 变量的 <code v-pre>embedData</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>body<span class="token operator">></span></span>
<span class="line">    Here is an image from raw data<span class="token punctuation">:</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>img src<span class="token operator">=</span><span class="token string double-quoted-string">"{{ <span class="token interpolation"><span class="token variable">$message</span><span class="token operator">-></span><span class="token property">embedData</span></span>(<span class="token interpolation"><span class="token variable">$data</span></span>, <span class="token interpolation"><span class="token variable">$name</span></span>) }}"</span><span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义-swiftmailer-消息" tabindex="-1"><a class="header-anchor" href="#自定义-swiftmailer-消息"><span>自定义 SwiftMailer 消息</span></a></h3>
<p><code v-pre>Mailable</code> 基类的 <code v-pre>withSwiftMessage</code> 方法允许你注册一个回调，它将在发送消息之前被调用，原始的 SwiftMailer 消息将作为该回调的参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">withSwiftMessage</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">addTextHeader</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Custom-Header'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'HeaderValue'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="markdown-格式的-mailables-类" tabindex="-1"><a class="header-anchor" href="#markdown-格式的-mailables-类"><span>Markdown 格式的 Mailables 类</span></a></h2>
<p>Markdown 格式 mailable 消息允许你从预构建模板和 mailable 类中的邮件通知组件获益。由于消息使用 Markdown 书写，Laravel 能够渲染出美观的、响应式的 HTML 模板消息，还能自动生成文本副本。</p>
<h3 id="生成-markdown-格式的-mailables-类" tabindex="-1"><a class="header-anchor" href="#生成-markdown-格式的-mailables-类"><span>生成 Markdown 格式的 Mailables 类</span></a></h3>
<p>要生成一个适用 Markdown 模板的 mailable，可以使用带有 <code v-pre>--markdown</code> 选项的 <code v-pre>make:mail</code> Artisan 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>mail OrderShipped <span class="token operator">--</span>markdown<span class="token operator">=</span>emails<span class="token operator">.</span>orders<span class="token operator">.</span>shipped</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，在它的 <code v-pre>build</code> 方法中配置 mailable，调用 <code v-pre>markdown</code> 方法代替 <code v-pre>view</code> 方法。 <code v-pre>markdown</code> 方法接受 Markdown 模板名和一个可选的在模板中可用的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 构建消息</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> $this</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails.orders.shipped'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写-markdown-消息" tabindex="-1"><a class="header-anchor" href="#编写-markdown-消息"><span>编写 Markdown 消息</span></a></h3>
<p>Markdown mailable 使用 Blade 组件和 Markdown 语法组合，让你可以更方便地利用 Laravel 预制组件构建邮件消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::message'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token comment"># 订单已发货</span></span>
<span class="line"></span>
<span class="line">Your order has been shipped<span class="token operator">!</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">View Order</span>
<span class="line">@endcomponent</span>
<span class="line"></span>
<span class="line">Thanks<span class="token punctuation">,</span><span class="token operator">&lt;</span>br<span class="token operator">></span></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：编写 Markdown 邮件时不要使用额外的缩进。Markdown 解析器将把缩进内容渲染成代码块。</p>
</blockquote>
<h4 id="按钮组件" tabindex="-1"><a class="header-anchor" href="#按钮组件"><span>按钮组件</span></a></h4>
<p>按钮组件渲染一个居中按钮链接。此组件接受两个参数， <code v-pre>url</code> 和可选的 <code v-pre>color</code>。颜色选项支持 <code v-pre>primary</code>、 <code v-pre>success</code> 和 <code v-pre>error</code>。你可以随心所欲地向消息添加任意数量的按钮组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::button'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'success'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">View Order</span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="面板组件" tabindex="-1"><a class="header-anchor" href="#面板组件"><span>面板组件</span></a></h4>
<p>面板组件在面板内渲染给定的文字块，面板与其他消息的背景色略有不同。能让你绘制一个警示文字块：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::panel'</span><span class="token punctuation">)</span></span>
<span class="line">This is the panel content<span class="token operator">.</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格组件" tabindex="-1"><a class="header-anchor" href="#表格组件"><span>表格组件</span></a></h4>
<p>表格组件允许你将 Markdown 表格转换成 HTML 表格。此组件接受 Markdown 表格作为其内容。列对齐支持默认的 Markdown 表格对齐语法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail::table'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Laravel</span>       <span class="token operator">|</span> <span class="token class-name">Table</span>         <span class="token operator">|</span> <span class="token class-name">Example</span>  <span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span> <span class="token operator">|</span><span class="token punctuation">:</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token punctuation">:</span><span class="token operator">|</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token punctuation">:</span><span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Col</span> <span class="token number">2</span> <span class="token class-name">is</span>      <span class="token operator">|</span> <span class="token class-name">Centered</span>      <span class="token operator">|</span> <span class="token variable">$10</span>      <span class="token operator">|</span></span>
<span class="line"><span class="token operator">|</span> <span class="token class-name">Col</span> <span class="token number">3</span> <span class="token class-name">is</span>      <span class="token operator">|</span> <span class="token class-name">Right</span><span class="token operator">-</span><span class="token class-name">Aligned</span> <span class="token operator">|</span> <span class="token variable">$20</span>      <span class="token operator">|</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义组件" tabindex="-1"><a class="header-anchor" href="#自定义组件"><span>自定义组件</span></a></h3>
<p>可以将所有 Markdown 邮件组件导出到自己的应用，用作自定义组件的模板。若要导出这些组件，使用带有 <code v-pre>laravel-mail</code> 资产标签的 <code v-pre>vendor:publish</code> Artisan 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>mail</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将 Markdown 邮件组件导出到 <code v-pre>resources/views/vendor/mail</code> 目录。 <code v-pre>mail</code> 目录包含 <code v-pre>html</code> 和 <code v-pre>text</code> 子目录， 分别包含各自对应的可用组件描述。可以按照自己的意愿自定义这些组件。</p>
<h4 id="自定义-css" tabindex="-1"><a class="header-anchor" href="#自定义-css"><span>自定义 CSS</span></a></h4>
<p>组件导出以后，<code v-pre>resources/views/vendor/mail/html/themes</code> 目录有一个 <code v-pre>default.css</code> 文件。可以自此文件中自定义 CSS，这些样式将自动内联到 Markdown 邮件消息的 HTML 表示中。</p>
<p>如果想为 Laravel 的 Markdown 组件构建一个全新的主题，您可以在 <code v-pre>html/themes</code> 目录中新建一个CSS文件。 命名并保存 CSS 文件后，并更新 <code v-pre>mail</code> 配置文件的 <code v-pre>theme</code> 选项以匹配新主题的名称。</p>
<p>要为单个 mailable 自定义主题，可以将 mailable 类的 <code v-pre>$theme</code> 属性设置为发送mailable 时应使用的主题名称。</p>
<h2 id="发送邮件" tabindex="-1"><a class="header-anchor" href="#发送邮件"><span>发送邮件</span></a></h2>
<p>若要发送邮件，使用 <code v-pre>Mail</code> <a href="https://learnku.com/docs/laravel/6.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 的 <code v-pre>to</code> 方法。 <code v-pre>to</code> 方法接受 邮件地址、用户实例或用户集合。如果传递一个对象或者对象集合，mailer 在设置收件人时将自动使用它们的 <code v-pre>email</code> 和 <code v-pre>name</code> 属性，因此请确保对象的这些属性可用。一旦指定了收件人，就可以将 mailable 类实例传递给 <code v-pre>send</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Order</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Mail</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 发送给定的订单。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">int</span></span>  <span class="token parameter">$orderId</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">ship</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$orderId</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$order</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">findOrFail</span><span class="token punctuation">(</span><span class="token variable">$orderId</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 发送订单...</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在发送消息时不止可以指定收件人。还可以通过链式调用「to」、「cc」、「bcc」一次性指定抄送和密送收件人：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="渲染-mailable" tabindex="-1"><a class="header-anchor" href="#渲染-mailable"><span>渲染 Mailable</span></a></h2>
<p>有时可能希望捕获 mailable 的 HTML 内容，而不发送它。可以调用 mailable 的 <code v-pre>render</code> 方法实现此目的。此方法返回 mailable 渲染计算后的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在浏览器中预览-mailable" tabindex="-1"><a class="header-anchor" href="#在浏览器中预览-mailable"><span>在浏览器中预览 Mailable</span></a></h3>
<p>设计 mailable 模板时，像 Blade 模板一样在浏览器中预览和渲染 mailable 是很方便的。这种情况下，Laravel 允许你在路由闭包或控制其中直接返回任意 mailable。返回的 mailable将在浏览器中渲染和显示，你可以快速预览设计效果，而不需要将其发送到真实的邮件地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mailable'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="邮件队列" tabindex="-1"><a class="header-anchor" href="#邮件队列"><span>邮件队列</span></a></h3>
<h4 id="将邮件消息加入队列" tabindex="-1"><a class="header-anchor" href="#将邮件消息加入队列"><span>将邮件消息加入队列</span></a></h4>
<p>由于发送邮件消息可能大幅度延长应用的响应时间，许多开发者选择将邮件消息加入队列放在后台发送。Laravel 使用内置的 <a href="https://learnku.com/docs/laravel/6.x/queues" target="_blank" rel="noopener noreferrer">统一队列 API</a> 简化了这一工作。若要将邮件消息加入队列，可以在指定消息的接收者后，使用 <code v-pre>Mail</code> facade 的 <code v-pre>queue</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此方法自动将作业推送到队列中以便消息在后台发送。使用此特性之前，需要 <a href="https://learnku.com/docs/laravel/6.x/queues" target="_blank" rel="noopener noreferrer">配置队列</a> ：</p>
<h4 id="延迟消息队列" tabindex="-1"><a class="header-anchor" href="#延迟消息队列"><span>延迟消息队列</span></a></h4>
<p>想要延迟发送队列化的邮件消息，可以使用 <code v-pre>later</code> 方法。<code v-pre>later</code> 方法的第一个参数的第一个参数是标示消息何时发送的 <code v-pre>DateTime</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$when</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">later</span><span class="token punctuation">(</span><span class="token variable">$when</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="推送到指定队列" tabindex="-1"><a class="header-anchor" href="#推送到指定队列"><span>推送到指定队列</span></a></h4>
<p>由于所有使用 <code v-pre>make:mail</code> 命令生成的 mailable 类都是用了 <code v-pre>Illuminate\Bus\Queueable</code> trait，因此你可以在任何 mailable 类实例上调用 <code v-pre>onQueue</code> 和 <code v-pre>onConnection</code> 方法来指定消息的连接和队列名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$message</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'emails'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token variable">$moreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">bcc</span><span class="token punctuation">(</span><span class="token variable">$evenMoreUsers</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">queue</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="默认队列" tabindex="-1"><a class="header-anchor" href="#默认队列"><span>默认队列</span></a></h4>
<p>如果一个 mailable 类终是要队列化，可以在此类上实现 <code v-pre>ShouldQueue</code> 契约。这样一来，即使你在发送时调用了 <code v-pre>send</code> 方法， mailable 也将被序列化：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">OrderShipped</span> <span class="token keyword">extends</span> <span class="token class-name">Mailable</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="本地化-mailable" tabindex="-1"><a class="header-anchor" href="#本地化-mailable"><span>本地化 Mailable</span></a></h2>
<p>Laravel 允许你使用有别于当前语言的区域设置发送 mailable，即使被加入到队列中也保留该区域设置。</p>
<p>为达到此目的， <code v-pre>Mail</code> facade 提供了 <code v-pre>locale</code> 方法设置目标语言。应用在格式化 mailable 是将切换到该区域设置，并在格式化完成后恢复到原来的区域设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">locale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'es'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="用户首选区域设置" tabindex="-1"><a class="header-anchor" href="#用户首选区域设置"><span>用户首选区域设置</span></a></h3>
<p>有时候，应用存储每个用户的首选区域设置。通过在一个或多个模型上实现 <code v-pre>HasLocalePreference</code> 契约，可以通知 Laravel 再发送邮件时使用预存的区域设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Translation<span class="token punctuation">\</span>HasLocalePreference</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token keyword">implements</span> <span class="token class-name">HasLocalePreference</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用户首选区域设置。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">preferredLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">locale</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦实现了此接口，Laravel 在向此模型发送 mailable 和通知时，将自动使用首选区域设置。因此在使用此接口时不需要调用 <code v-pre>locale</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Mail</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">OrderShipped</span><span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="邮件-本地开发" tabindex="-1"><a class="header-anchor" href="#邮件-本地开发"><span>邮件 &amp; 本地开发</span></a></h2>
<p>在开发发送邮件的应用时，你也许不想真的向实时邮件地址发送邮件。Laravel 为本地开发期间提供了几个 「禁用」真实发送的途径。</p>
<h4 id="日志驱动" tabindex="-1"><a class="header-anchor" href="#日志驱动"><span>日志驱动</span></a></h4>
<p><code v-pre>log</code> 邮件驱动采取将邮件消息写入日志取代发送邮件，已备查看。应用环境配置的更多消息，请查阅 <a href="https://learnku.com/docs/laravel/6.x/configuration#environment-configuration" target="_blank" rel="noopener noreferrer">配置文档</a>。</p>
<h4 id="通用配置" tabindex="-1"><a class="header-anchor" href="#通用配置"><span>通用配置</span></a></h4>
<p>Laravel 为通过框架发送的邮件提供了指定常用收件人的其他解决方案。通过此方法，应用生成的邮件都将发送到指定地址，以取代发送消息时指定的真实地址。可以借助 <code v-pre>config/mail.php</code> 配置文件的 <code v-pre>to</code> 选项实现此目的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'to'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Example'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="mailtrap" tabindex="-1"><a class="header-anchor" href="#mailtrap"><span>Mailtrap</span></a></h4>
<p>最后，你可以使用 <a href="https://mailtrap.io/" target="_blank" rel="noopener noreferrer">Mailtrap</a> 服务和 <code v-pre>smtp</code> 驱动发送邮件消息到 「虚拟」邮箱，这样就可以在真实的邮件客户端查看邮件消息。此方法的好处是允许你在 Mailtrap 的消息阅览器中实际查看最终的邮件。r.</p>
<h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>Laravel 在处理邮件消息发送时触发两个事件。<code v-pre>MessageSending</code> 事件在消息发送前触发，<code v-pre>MessageSent</code> 事件则在消息发送后触发。切记，这些事件是在邮件被 <em>发送</em> 时触发，而不是在队列化的时候。可以在 <code v-pre>EventServiceProvider</code> 中注册此事件的侦听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 为应用映射事件侦听器。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Illuminate\Mail\Events\MessageSending'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'App\Listeners\LogSendingMessage'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'Illuminate\Mail\Events\MessageSent'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'App\Listeners\LogSentMessage'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/mail/5165" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/mai...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/mail/5165" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/mai...</a></p>
</blockquote>
</div></template>


